package RSSReader;

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;


/**
 *
 * @author Christian
 */
public class httpRequest
{
    HttpConnection httpConn = null;
    String url = "http://www.google.de";
    InputStream in = null;
    OutputStream out = null;
    String connectionType = "GET";
    StringBuffer sb;
    
    
    public httpRequest()
    {
        //this.fetchSite("http://www.google.de");
    }


    // HTTP STREAM ZIEHEN
    public InputStream getHTTPStream(String url)
    {
        HttpConnection hc = null;
        InputStream httpInputStream = null;

        try
        {
          hc = (HttpConnection)Connector.open(url);
          httpInputStream = hc.openInputStream(); // STREAM ZURÜCKGEBEN
        }
        catch (IOException ioe) {}
        finally
        {
          try
          {
              if (hc != null) { hc.close(); }
          }
          catch (IOException ignored) { }  
        }

        return httpInputStream;
    }


    // STRING VON GET REQUEST ZIEHEN
    public String fetchSite(String url)
    {
        HttpConnection httpConn = null;
        InputStream is = null;
        String dataRead = "";
        try
        {
            httpConn = (HttpConnection)Connector.open( url );
            if((httpConn.getResponseCode() == HttpConnection.HTTP_OK))
            {
                int length = (int)httpConn.getLength();
                is = httpConn.openInputStream();
                if(length == -1)
                {
                    int chunkSize = 1500;
                    byte[] data = new byte[chunkSize];
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    int dataSizeRead = 0;
                    while((dataSizeRead = is.read(data))!= -1)
                    {
                        baos.write(data, 0, dataSizeRead );
                    }
                    dataRead = new String(baos.toByteArray());
                    baos.close();
                }
                else
                {
                    DataInputStream dis = new DataInputStream(is);
                    byte[] data = new byte[length];

                    dis.readFully(data);
                    dataRead = new String(data);
                }
            }
            else
            {
                dataRead = "";
            }
        }
        catch(Throwable t)
        {
            dataRead = "";
        }

        finally
        {
            try
            {
                if(is != null)
                    is.close();
            }
            catch(Throwable t)
            {
            }
            try
            {
                if(httpConn != null)
                    httpConn.close();
            }
            catch(Throwable t)
            {
            }
        }
        return this.convertWebStream(dataRead);
    }


    public String convertWebStream(String http)
    {
        http = this.replace("&auml;", "ä", http);
        http = this.replace("&Auml;", "Ä", http);
        http = this.replace("&ouml;", "ö", http);
        http = this.replace("&Ouml;", "Ö", http);
        http = this.replace("&uuml;", "ü", http);
        http = this.replace("&Uuml;", "Ü", http);
        http = this.replace("&szlig;", "ß", http);
        http = this.replace("â", "-", http);
        http = this.replace("&amp;", "&", http);
        http = this.replace("&quot;", "\"", http);
        return http;
    }

    private String replace(String needle, String replacement, String haystack)
    {
        String result = "";
        int index = haystack.indexOf(needle);
        if(index==0)
        {
            result = replacement+haystack.substring(needle.length());
            return replace(needle, replacement, result);
        }
        else if(index>0)
        {
            result = haystack.substring(0,index)+ replacement +haystack.substring(index+needle.length());
            return replace(needle, replacement, result);
        }
        else
            return haystack;
    }
}
